/**
 * Created by ly on 2015/7/23.
 */
"use strict";

var fs = require("fs");
module.exports = function (grunt) {

    //加载所有任务
    require("load-grunt-tasks")(grunt);

    //显示任务执行消耗的时间
    require("time-grunt")(grunt);

    //任务配置数据
    grunt.initConfig({
        pkg: grunt.file.readJSON("package.json"),
        clean: {
            server: {
                files: [{
                    src: [
                        "./dist"
                    ]
                }]
            }
        },
        less: {
            development: {
                options: {
                    modifyVars: {
                        theme: ""
                    },
                    compress: false
                },
                files: [{
                    expand: true,
                    cwd: "./app/assets/less",
                    src: ["style.less"],
                    dest: "./app/assets/css",
                    ext: ".css"
                }]
            },
            production: {
                options: {
                    modifyVars: {
                        theme: ""
                    },
                    compress: true
                },
                files: [{
                    expand: true,
                    cwd: "./app/assets/less",
                    src: ["style.less"],
                    dest: "./app/assets/css",
                    ext: ".css"
                }]
            }
        },
        concat: {
            options: {
                separator: ";"
            },
            dist: {
                files: {
                    //"./app/assets/js/component.js": ["./app/assets/js/nav.js","./app/assets/js/slide.js","./app/assets/js/table.js"],
                    //"./app/assets/js/plugin.js": ["bootstrap/dist/css/bootstrap.css","jquery/jquery.js"]
                }
            }
        },
        uglify: {
            options: {
                mangle: false,
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
            },
            development: {
                files: [{
                    expand: true,
                    cwd: "./app/assets/js",
                    src: ["component.js", "plugin.js"],
                    dest: "./app/assets/minjs"
                }]
            }
        },
        watch: {
            options: {
                livereload: true
            },
            scripts: {
                options: {
                    spawn: false
                },
                files: ["./app/assets/less/**/*.less", "./app/assets/less/*.less"],
                tasks: ["less"]
            },
            server: {
                files: ['.nodemon']
            }
        },
        nodemon: {
            dev: {
                script: "./app/server.js",
                options: {
                    args: [],
                    nodeArgs: ["--harmony", "--debug"],
                    callback: function (nodemon) {
                        fs.writeFileSync('.nodemon', 'started');
                        nodemon.on('log', function (event) {
                            console.log(event.colour);
                        });
                        nodemon.on('restart', function () {
                            setTimeout(function () {
                                fs.writeFileSync('.nodemon', 'restarted');
                            }, 250);
                        });
                    },
                    env: {
                        PORT: "8181",
                        NODE_ENV: "development"
                    },
                    cwd: __dirname,
                    ignore: ["node_modules/**"],
                    ext: "js,coffee",
                    watch: ["server"],
                    delay: 1000,
                    legacyWatch: true
                }
            }
        },
        copy: {
            assets: {
                files: [
                    {
                        expand: true,
                        src: "./js/*.js",
                        dest: "./dist/app/assets"
                    },
                    {
                        expand: true,
                        src: "./css/*.css",
                        dest: "./dist/app/assets"
                    }
                ]
            },
            all: {
                files: [{
                    expand: true,
                    cwd: "./",
                    src: [
                        "*.sh",
                        "*.json",
                        "app/**/*",
                        "!app/assets/{,**/}*.less",
                    ],
                    dest: "./dist"
                }]
            }
        },
        wiredep: {
            app: {
                src: "app/views/{,*/}*.html",
                ignorePath: /(\.\.\/)+bower_components\//
            }
        },
        htmlUsemin: {
            main: {
                options: {
                    tasks: {
                        js: ["concat", "uglify", "filerev"],
                        css: ["concat", "autoprefixer", "cssmin", "filerev"]
                    },
                    dirTasks: "filerev",
                    prefix: "",
                    targetPrefix: "./dist/app/assets",
                    replacePath: {
                        "#{env}": "dist"
                    },
                    blockReplacements: {
                        js: function (block) {
                            return '<script src="' + block.dest + '"></script>';
                        },
                        css: function (block) {
                            return '<link rel="stylesheet" href="' + block.dest + '">';
                        }
                    }
                },
                files: [{
                    expand: true,
                    cwd: "./app/views",
                    src: ["**/*.html"],
                    dest: "dist/app/views"
                }]
            }
        },
        auto_install: {
            production: {
                options: {
                    cwd: './dist',
                    stdout: true,
                    stderr: true,
                    failOnError: true,
                    npm: '--production'
                }
            }
        },
        concurrent: {
            tasks: ['nodemon:dev', 'watch:server'],
            options: {
                logConcurrentOutput: true
            }
        },
        zip: {
            //'repository/rtm-portal.zip': 'dist/**'
            "zip": {
                cwd: 'dist/',
                src: 'dist/**',
                dest: 'dest/alphacar_<%= grunt.template.today("yyyy_mm_dd") %>.zip'
            }
        }
    });
    grunt.registerTask("package", ["clean:server", "less:production", "copy:all", "copy:assets", "htmlUsemin:main", "zip:zip"]);
};